# coding:utf-8
# 爬取电影 脚本
# -db
#   -conf.ini
#   -data_back 存放数据
# -model.py 爬虫
# -readconf.py 读取爬取页数
# todo 爬取一段时间被禁止了 修改header后 目前未发现问题

from sqlalchemy.sql.functions import random
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
import time
from urllib import request
import json
import random
from readconf import ConfParser

import http.client
http.client.HTTPConnection._http_vsn = 10
http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
# 以上3行解决报错  http.client.IncompleteRead: IncompleteRead(0 bytes read)


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./db/data_back.db'#'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
app.config['SQLALCHEMY_ECHO'] = False

def get_time():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

class User(db.Model):
    __tablename__ = 'User'
    id = db.Column(db.INTEGER, primary_key=True)
    name = db.Column(db.String(50))
    email = db.Column(db.String(50))
    password = db.Column(db.String(50))
    admin = db.Column(db.Boolean)
    image = db.Column(db.String(500))
    create_at = db.Column(db.String(50), default=get_time())


class Blog(db.Model):
    __tablename__ = 'blog'
    id = db.Column(db.INTEGER, primary_key=True)
    user_id = db.Column(db.String(50))
    user_name = db.Column(db.String(50))
    user_image = db.Column(db.String(500))
    name = db.Column(db.String(50))
    summary = db.Column(db.String(200))
    content = db.Column(db.TEXT)
    create_at = db.Column(db.DateTime, default=time.time)


class Comment(db.Model):
    id = db.Column(db.INTEGER,primary_key=True)
    blog_id = db.Column(db.String(50))
    user_id = db.Column(db.String(50))
    user_image = db.Column(db.String(500))
    content = db.Column(db.TEXT)
    create_at = db.Column(db.DateTime, default=time.time)


class vod_type(db.Model):
    __tablenname__ = 'vod_type'
    id = db.Column(db.INTEGER, primary_key=True)
    type_id = db.Column(db.String(10))
    type_name = db.Column(db.String(50))


class vod_detail(db.Model):
    __tablename__ = 'vod_detail'
    id = db.Column(db.INTEGER, primary_key=True)
    vod_id = db.Column(db.String(50))
    type_id = db.Column(db.String(50))
    type_id_1 = db.Column(db.String(50))
    group_id = db.Column(db.String(50))
    vod_name = db.Column(db.String(50))
    vod_sub = db.Column(db.String(50))
    vod_en = db.Column(db.String(50))
    vod_status = db.Column(db.String(50))
    vod_letter = db.Column(db.String(50))
    vod_tag = db.Column(db.String(50))
    vod_class = db.Column(db.String(50))
    vod_pic = db.Column(db.String(50))
    vod_pic_thumb = db.Column(db.String(50))
    vod_pic_slide = db.Column(db.String(50))
    vod_actor = db.Column(db.String(50))
    vod_director = db.Column(db.String(50))
    vod_writer = db.Column(db.String(50))
    vod_behind = db.Column(db.String(50))
    vod_blurb = db.Column(db.String(50))
    vod_remarks = db.Column(db.String(50))
    vod_pubdate = db.Column(db.String(50))
    vod_total = db.Column(db.String(50))
    vod_serial = db.Column(db.String(50))
    vod_tv = db.Column(db.String(50))
    vod_weekday = db.Column(db.String(50))
    vod_area = db.Column(db.String(50))
    vod_lang = db.Column(db.String(50))
    vod_version = db.Column(db.String(50))
    vod_state = db.Column(db.String(50))
    vod_author = db.Column(db.String(50))
    vod_jumpurl = db.Column(db.String(50))
    vod_tpl = db.Column(db.String(50))
    vod_tpl_play = db.Column(db.String(50))
    vod_tpl_down = db.Column(db.String(50))
    vod_isend = db.Column(db.String(50))
    vod_lock = db.Column(db.String(50))
    vod_level = db.Column(db.String(50))
    vod_copyright = db.Column(db.String(50))
    vod_points = db.Column(db.String(50))
    vod_points_play = db.Column(db.String(50))
    vod_points_down = db.Column(db.String(50))
    vod_hits = db.Column(db.String(50))
    vod_hits_day = db.Column(db.String(50))
    vod_hits_week = db.Column(db.String(50))
    vod_hits_month = db.Column(db.String(50))
    vod_duration = db.Column(db.String(50))
    vod_up = db.Column(db.String(50))
    vod_down= db.Column(db.String(100))
    vod_score = db.Column(db.String(100))
    vod_score_all = db.Column(db.String(100))
    vod_score_num= db.Column(db.String(100))
    vod_time = db.Column(db.String(100))
    vod_time_add = db.Column(db.String(100))
    vod_time_hits = db.Column(db.String(100))
    vod_time_make = db.Column(db.String(100))
    vod_trysee = db.Column(db.String(100))
    vod_douban_id = db.Column(db.String(100))
    vod_douban_score = db.Column(db.String(100))
    vod_reurl = db.Column(db.String(100))
    vod_rel_vod = db.Column(db.String(100))
    vod_rel_art = db.Column(db.String(100))
    vod_pwd = db.Column(db.String(100))
    vod_pwd_url = db.Column(db.String(100))
    vod_pwd_play = db.Column(db.String(100))
    vod_pwd_play_url = db.Column(db.String(100))
    vod_pwd_down = db.Column(db.String(100))
    vod_pwd_down_url = db.Column(db.String(100))
    vod_content = db.Column(db.String(100))
    vod_play_from = db.Column(db.String(100))
    vod_play_server = db.Column(db.String(100))
    vod_play_note = db.Column(db.String(100))
    vod_play_url = db.Column(db.String(100))
    vod_down_from = db.Column(db.String(100))
    vod_down_server = db.Column(db.String(100))
    vod_down_note = db.Column(db.String(100))
    vod_down_url = db.Column(db.String(200))
    type_name = db.Column(db.String(100))



class vod(db.Model):
    __tablenname__ = 'vod'
    id = db.Column(db.INTEGER, primary_key=True)
    vod_id = db.Column(db.String(20))
    vod_name = db.Column(db.String(60))
    type_id = db.Column(db.String(5))
    type_name = db.Column(db.String(50))
    vod_en = db.Column(db.String(80))
    vod_time = db.Column(db.DateTime)
    vod_remarks = db.Column(db.String(50))
    vod_play_from = db.Column(db.String(60))




def get_detail(ob):
    print('add %s  ...' % ob['vod_id'])
    r = vod_detail(vod_id = ob['vod_id'],
        type_id = ob['type_id'],
    type_id_1 = ob['type_id_1'],
    group_id = ob['group_id'],
    vod_name = ob['vod_name'],
    vod_sub = ob['vod_sub'],
    vod_en = ob['vod_en'],
    vod_status = ob['vod_status'],
    vod_letter = ob['vod_letter'],
    vod_tag = ob['vod_tag'],
    vod_class = ob['vod_class'],
    vod_pic = ob['vod_pic'],
    vod_pic_thumb = ob['vod_pic_thumb'],
    vod_pic_slide = ob['vod_pic_slide'],
    vod_actor = ob['vod_actor'],
    vod_director = ob['vod_director'],
    vod_writer = ob['vod_writer'],
    vod_behind = ob['vod_behind'],
    vod_blurb = ob['vod_blurb'],
    vod_remarks = ob['vod_remarks'],
    vod_pubdate = ob['vod_pubdate'],
    vod_total = ob['vod_total'],
    vod_serial = ob['vod_serial'],
    vod_tv = ob['vod_tv'],
    vod_weekday = ob['vod_weekday'],
    vod_area = ob['vod_area'],
    vod_lang = ob['vod_lang'],
    vod_version = ob['vod_version'],
    vod_state = ob['vod_state'],
    vod_author = ob['vod_author'],
    vod_jumpurl = ob['vod_jumpurl'],
    vod_tpl = ob['vod_tpl'],
    vod_tpl_play = ob['vod_tpl_play'],
    vod_tpl_down = ob['vod_tpl_down'],
    vod_isend = ob['vod_isend'],
    vod_lock = ob['vod_lock'],
    vod_level = ob['vod_level'],
    vod_copyright = ob['vod_copyright'],
    vod_points = ob['vod_points'],
    vod_points_play = ob['vod_points_play'],
    vod_points_down = ob['vod_points_down'],
    vod_hits = ob['vod_hits'],
    vod_hits_day = ob['vod_hits_day'],
    vod_hits_week = ob['vod_hits_week'],
    vod_hits_month = ob['vod_hits_month'],
    vod_duration = ob['vod_duration'],
    vod_up = ob['vod_up'],
    vod_down= ob['vod_down'],
    vod_score = ob['vod_score'],
    vod_score_all = ob['vod_score_all'],
    vod_score_num= ob['vod_score_num'],
    vod_time = ob['vod_time'],
    vod_time_add = ob['vod_time_add'],
    vod_time_hits = ob['vod_time_hits'],
    vod_time_make = ob['vod_time_make'],
    vod_trysee = ob['vod_trysee'],
    vod_douban_id = ob['vod_douban_id'],
    vod_douban_score = ob['vod_douban_score'],
    vod_reurl = ob['vod_reurl'],
    vod_rel_vod = ob['vod_rel_vod'],
    vod_rel_art = ob['vod_rel_art'],
    vod_pwd = ob['vod_pwd'],
    vod_pwd_url = ob['vod_pwd_url'],
    vod_pwd_play = ob['vod_pwd_play'],
    vod_pwd_play_url = ob['vod_pwd_play_url'],
    vod_pwd_down = ob['vod_pwd_down'],
    vod_pwd_down_url = ob['vod_pwd_down_url'],
    vod_content = ob['vod_content'],
    vod_play_from = ob['vod_play_from'],
    vod_play_server = ob['vod_play_server'],
    vod_play_note = ob['vod_play_note'],
    vod_play_url = ob['vod_play_url'],
    vod_down_from = ob['vod_down_from'],
    vod_down_server = ob['vod_down_server'],
    vod_down_note = ob['vod_down_note'],
    vod_down_url = ob['vod_down_url'],
    type_name = ob['type_name'])

    db.session.add(r)
    db.session.commit()
    print('end ....')

def get_lists(page):
    print("get lists")
    time.sleep(random.randint(3, 5))
    url = base_url + '&pg=' + str(page)
    res = request.urlopen(url)
    j_res = json.loads(res.readline().decode('utf-8'))  #
    lists = j_res['list']
    # print(lists)
    return lists

# get_detail()

def get_page(pagecount,beginpage):  # 2443
    print("current pagenum: %s, begin page: %s" % (pagecount,beginpage))
    for m in range(beginpage, pagecount+1):
        time.sleep(3)
        lists = get_lists(m)
        print('###################### get page %s ############################ ' % m)
        for i in lists:
            get_detail(i)


base_url = "https://api.okzy.tv/api.php/provide/vod/?ac=detail"
headers = {'User-Agent':'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0',
           'Accept-Language':'zh-CN,zh;q=0.9','Connection':'keep-alive','Accept-Encoding':'gzip, deflate, br'}

def get_pagecount():
    print("获取总页数")
    url = base_url
    res = request.urlopen(url)
    j_res = json.loads(res.readline().decode('utf-8'))
    pagecount = j_res['pagecount']
    print("总页数为：%s" % pagecount)
    return pagecount


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/comedy')
def comey():
    #movies = vod_detail.query.filter(type_id='7').first()
#       查询 user 表里面名字等于 Tom 的：
#         db.session.query(User).filter(User.name == 'Tom').all()
#        查询 user 表里面名字等于 Tom 并且年龄等于 18：
#         db.session.query(User).filter(User.name == 'Tom', User.age == 18).all()
#        也可以这样：
#         db.session.query(User).filter(User.name == 'Tom'）.filter(User.age == 18).all()
#        如果想使用 and 拼接需要用以下方式：
#         db.session.query(User).filter(and_(User.name == 'Tom', User.age == 18)).all()
#        以下的方式 and 后面的 User.age == 18 不会生效：
#         db.session.query(User).filter(User.name == 'Tom' and User.age == 18).all()
#        查询 user 表里面名字等于 Tom 的或者年龄等于 18：
#         db.session.query(User).filter(or_(User.name == 'Tom', User.age == 18)).all()
#        查询 user 表里面名字等于 Tom 的并且年龄大于 18
#         db.session.query(User).filter(User.name == 'Tom', User.age > 18).all()

    movies = db.session.query(vod_detail).filter(vod_detail.type_id == '7').limit(24).all()
    print(movies)
    return render_template('comedy.html', movies=movies)


@app.route('/movie/<movie_id>')
def movie(movie_id):

    vod_data = db.session.query(vod_detail).filter(vod_detail.vod_id == movie_id).first()
    s = vod_data.vod_play_url.split('$$$')[0].split('$')
    s1_qxd = s[0]
    s1_url = s[1]
    return render_template('movie.html', movie_url=s1_url, vod=vod_data) #render_template('movie.html', vod_data=vod_data)

@app.route('/api/users')
def users():
    str = '''[
      {"key": '111',
      "name": 'John Brown11',
      "age": "32",
      "address": 'New York No. 1 Lake Park',
    },
    {
      "key": '211',
      "name": 'Jim Green11',
      "age": "42",
      "address": 'London No. 1 Lake Park',
    }
           '''
    return str

if __name__ == '__main__':
    # 爬取数据

    print("begin crawe...")
    #get_pagecount()

    cf = ConfParser()
    oldpages = int(cf.get_value(key="COUNTS"))
    newpages = get_pagecount()
    if newpages > oldpages:
        get_page(newpages, oldpages)
        cf.set_value("COUNTS", str(newpages))

    else:
        print("无更新。。。。。。。。。")


    # get_page(2456)

    #app.run(debug=True,host='0.0.0.0')






